﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AspNetTraining.BLL
{
    public class GroupUserManager
    {
        /// <summary>
        /// Get Name Group User by Id
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public String GetGroupUserNameByID(System.Guid ID)
        {
            using (var context = new DotNetTrainingEntities())
            {
                var name = context.GroupUsers.Where(gr => gr.GroupUserID == ID).FirstOrDefault();
                try { return name.GroupName.ToString(); }
                catch { return string.Empty; }
               
            }
        }
        /// <summary>
        /// Get all GroupUser
        /// </summary>
        /// <returns></returns>
        public List<GroupUser> GetGroupUser()
        {
           using (var context = new DotNetTrainingEntities())
            {
                var name = context.GroupUsers;
                return name.ToList<GroupUser>();
            }
        }
        /// <summary>
        /// Group User
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public GroupUser GetGroupUserByID(System.Guid ID)
        {
            using (var context = new DotNetTrainingEntities())
            {
                return context.GroupUsers.Where(gr => gr.GroupUserID == ID).FirstOrDefault();            
            }
        }
        /// <summary>
        /// Them moi Insert new Group User
        /// </summary>
        /// <param name="guser"></param>
        /// <returns></returns>
        public bool InsertGroupUser(GroupUser guser)
        {
            using (var context = new DotNetTrainingEntities())
            {
                context.GroupUsers.Add(guser);
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch { return false; }
            }
        }
        /// <summary>
        /// Update Group User
        /// </summary>
        /// <param name="guser"></param>
        /// <returns></returns>
        public bool UpdateGroupUser(GroupUser guser)
        {
            using (var context = new DotNetTrainingEntities())
            {
                context.Entry(guser).State = EntityState.Modified;
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch { return false; }
            }
        }
        /// <summary>
        /// delete group by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteGroupAccount(System.Guid id)
        {
            using (var context = new DotNetTrainingEntities())
            {
                try
                {
                    GroupUser user = context.GroupUsers.Where(scc => scc.GroupUserID == id).FirstOrDefault();
                    //kiem tra nhom he thong
                    if (user.IsAdmin == true)
                        return false;
                    context.Entry(user).State = EntityState.Deleted;
                    context.SaveChanges();
                    return true;
                }
                catch { return false; }
            }
        }
    }
}
